import { _decorator, Component, Node, instantiate, loader, LightComponent, SkeletalAnimationComponent, CameraComponent } from "cc";
import { ResMgr } from "../Managers/ResMgr";
import { EventMgr } from "../Managers/EventMgr";
import { SoundMgr } from "../Managers/SoundMgr";
import { UIMgr } from "../Managers/UIMgr";
import { ShootCtrl } from "./ShootCtrl";
import { EnmeyMgr } from "./EnmeyMgr";
//特价9.9元一套cocoscreator代码联系Q2483367084 
//截图https://share.weiyun.com/leGAHpnB 密码:b9udtv
const { ccclass, property } = _decorator;

var resPkg = {
    scenes_3D: [
        "map/Level1",
        "Env/Env",
    ],

    charactors_3D: [
        "charctors/enemy",
    ],

    UI_atlas: [
    ],

    Sounds: [
        "sounds/BGM0",
        "sounds/Lose",
        "sounds/M60_fire",
        "sounds/Pistol_magin",
        "sounds/Smash",
    ],

    UI_prefabs: [
        "ui_prefabs/MainUI",
    ]
};

@ccclass("Game")
export class Game extends Component {
    public static Instance: Game = null;
    private loadingUI: Node = null;

    private shooter: Node = null;
    private shootCtrl: ShootCtrl = null;

    private showPlayer: Node = null;
    private sunLight: LightComponent = null; 
    private monestRoot: Node = null;
    private gameCamera: CameraComponent = null;
    private enmeyCamera: CameraComponent = null;
    private gameStarted: boolean = false;
    private roadEditor: Node = null;
    private isEnemyGenEnd: boolean = false;


    onLoad(): void {
        if (Game.Instance === null) {
            Game.Instance = this;
        }
        else {
            this.destroy();
            return;
        }

        EventMgr.Instance.add_event_listenner("PlayerDead", this, this.onPlayerDead);
        EventMgr.Instance.add_event_listenner("GenEnemyEnd", this, this.onGenEnmeyEnd);
        this.node.addComponent(EnmeyMgr);
    }

    onGenEnmeyEnd(): void { // 提示一下，敌人生成完成了；
        this.isEnemyGenEnd = true;
    }

    // 游戏失败
    private onPlayerDead(): void {
        this.unscheduleAllCallbacks();
        console.log("游戏失败");
    }

    // 敌人全部生成完成, 所有敌人都被干掉;
    private onPlayerWin(): void { 
        console.log("玩家胜利");
    }

    gameStart(): void {
        this.loadingUI = UIMgr.Instance.show_ui("ResLoading") as Node;

        ResMgr.Instance.preloadResPackage(resPkg, function(now, total) {
            EventMgr.Instance.dispatch_event("loadProgress", now / total);
        }, function() {
            this.onResLoaded();
        }.bind(this));
    }

    onResLoaded(): void {
        this.EnterGame();

        this.loadingUI.removeFromParent();
        ResMgr.Instance.releaseResPackage({UI_prefabs: ["ui_prefabs/ResLoading"]});
    }

    // 进入游戏放游戏内容
    EnterGame(): void {
        // 放我们的地图
        var map_prefab = ResMgr.Instance.getAsset("map/Level1");
        var map = instantiate(map_prefab);
        this.node.addChild(map);

        this.roadEditor = map.getChildByName("roadEditor");
        // end 

        // 放我们的游戏环境物体
        var env_prefab = ResMgr.Instance.getAsset("Env/Env");
        var env = instantiate(env_prefab);
        this.node.addChild(env);

        this.shooter = env.getChildByName("Shooter"); // Env/Shooter节点
        this.shootCtrl = this.shooter.addComponent(ShootCtrl);

        this.gameCamera = env.getChildByName("GameCamera").getComponent(CameraComponent);
        this.shootCtrl.bindCamera(this.gameCamera);
        this.enmeyCamera = env.getChildByName("EnemyCamera").getComponent(CameraComponent);

        this.showPlayer = env.getChildByName("ShowPlayer");
        this.sunLight = env.getChildByName("Main Light").getComponent(LightComponent);
        this.monestRoot = env.getChildByName("monestRoot");
        // end 

        // 释放我们的UI出来;
        UIMgr.Instance.show_ui("MainUI");
        // 

        this.EnterStart();
    }

    public EnterStart(): void {
        this.shooter.active = false;
        this.showPlayer.active = true;

        // this.gameCamera.node.active = true; // 玩家视角的游戏摄像机
        // this.enmeyCamera.node.active = false; // 敌人视角的摄像机

        var anim = this.showPlayer.getComponent(SkeletalAnimationComponent); 
        anim.stop();
        anim.play();

        this.monestRoot.removeAllChildren();
        SoundMgr.Instance.play_music("sounds/BGM0", true);

        EventMgr.Instance.dispatch_event("UIEvent_EnterStart", null);
    }

    public EnterGameMode(): void {
        if (this.gameStarted === true) {
            return;
        }

        // this.gameCamera.node.active = false; // 玩家视角的游戏摄像机
        // this.enmeyCamera.node.active = true; // 敌人视角的摄像机
        // console.log(this.enmeyCamera.node.worldPosition, this.enmeyCamera.node.eulerAngles);
        
        this.gameStarted = true;
        this.shooter.active = true;

        this.showPlayer.active = false;

        this.monestRoot.removeAllChildren();
        SoundMgr.Instance.stop_music();

        // 播放一个拉抢的声音
        SoundMgr.Instance.play_effect("sounds/Pistol_magin");
        // end 

        EventMgr.Instance.dispatch_event("UIEvent_EnterGame", null);

        // 加载关卡数据
        this.isEnemyGenEnd = false;
        EnmeyMgr.Instance.loadLevelData(1, this.roadEditor);
        // end

        // 初始化玩家的数据, 也可以从表格里面根据玩家等级来加载;
        this.shootCtrl.initPlayer({hp: 100, killVlaue: 10,});
        // end 

        this.unscheduleAllCallbacks();

        this.scheduleOnce(function() { // 出兵
            EnmeyMgr.Instance.genEnmeies(this.monestRoot);
        }.bind(this), 3);

        this.scheduleOnce(this.checkGame.bind(this), 3);
    }

    private checkGame(): void {
        if (this.isEnemyGenEnd && this.monestRoot.children.length <= 0) {
            this.onPlayerWin();
            return;
        }

        this.scheduleOnce(this.checkGame.bind(this), 3);
    }

    onDestroy(): void {
        EventMgr.Instance.remove_event_listenner("PlayerDead", this, this.onPlayerDead);
    }
}
